package com.young.mylib.db.dao;

import android.util.Log;

import com.young.mylib.db.DatabaseUtils;
import com.young.mylib.db.entity.DeviceDataEntity;
import com.young.mylib.db.entity.DeviceEntity;
import com.young.mylib.db.entity.DeviceRealtimeDataEntity;
import com.young.mylib.db.entity.LinkageEntity;
import com.young.mylib.db.entity.LinkageLog;

import java.util.List;

/**
 * Created by Young on 2018/11/5.
 */

public class DeviceDao extends BaseDao {
    private final static String TAG = "DeviceDao";

    public static void dropDeviceTable() {
        String sql = "DROP TABLE IF EXISTS DeviceEntity";
        DatabaseUtils.getHelper().rawQuery(DeviceEntity.class, sql);
    }

    public static void dropLinkageTable() {
        String sql = "DROP TABLE IF EXISTS LinkageEntity";
        DatabaseUtils.getHelper().rawQuery(LinkageEntity.class, sql);
    }

    public static void dropDeviceDataTable() {
        String sql = "DROP TABLE IF EXISTS DeviceDataEntity";
        DatabaseUtils.getHelper().rawQuery(DeviceDataEntity.class, sql);
    }

    public static void saveDevice(DeviceEntity entity) {
        DatabaseUtils.getHelper().save(entity);
    }
    public static void saveLinkageLog(LinkageLog entity) {
        DatabaseUtils.getHelper().save(entity);
    }

    public static void saveDeviceData(DeviceDataEntity entity) {
        DatabaseUtils.getHelper().save(entity);
    }

    /**
     * todo 待优化
     *
     * @param entity
     */
    public static void updateDeviceRealtimeData(DeviceDataEntity entity) {
        DeviceRealtimeDataEntity entity1 = new DeviceRealtimeDataEntity();
        entity1.deviceId = entity.deviceId;
        entity1.deviceName = entity.deviceName;
        entity1.deviceType = entity.deviceType;
        entity1.lightStatus = entity.lightStatus;
        entity1.ts = entity.ts;
        entity1.time = entity.time;
        DatabaseUtils.getHelper().updateByKey(entity1, "deviceId", entity1.deviceId);
    }

    public static void saveLinkage(LinkageEntity entity) {
        DatabaseUtils.getHelper().save(entity);
    }

    public static void replaceLinkage(LinkageEntity entity) {
        String treeId = entity.treeId;
        List<LinkageEntity> linkages = DatabaseUtils.getHelper().queryByKey(LinkageEntity.class, "treeId", treeId);
        if (linkages.size() > 0) {
            DatabaseUtils.getHelper().updateByKey(entity, "treeId", treeId);
        } else {
            DatabaseUtils.getHelper().save(entity);
        }

    }

    public static List<DeviceEntity> findAll() {
        return DatabaseUtils.getHelper().queryAll(DeviceEntity.class);
    }
    public static DeviceEntity findDevice(String deviceId) {
        List<DeviceEntity> list=DatabaseUtils.getHelper().queryByKey(DeviceEntity.class,"deviceId",deviceId);
      //  Log.i(TAG,"查找设备："+deviceId+":"+list.size());
        if (list.size() == 0) {            ;
            return null;
        }
        return list.get(0);
    }


    public static List<DeviceDataEntity> findAllData() {
        return DatabaseUtils.getHelper().queryAll(DeviceDataEntity.class);
    }
    public static List<DeviceDataEntity> findDeviceDataByRange(long start,long end) {
        String sql = "SELECT * FROM DeviceDataEntity  WHERE ts >= '" + start + "' and ts <= '"+end+"'";
        Log.i(TAG,"findDataByRange "+sql);
        return DatabaseUtils.getHelper().rawQuery(DeviceDataEntity.class, sql);
    }
    public static List<DeviceDataEntity> findDataByRange(long ts) {
        String sql = "SELECT * FROM DeviceDataEntity  WHERE ts >= '" + ts + "'";
        Log.i(TAG,"findDataByRange "+sql);
        return DatabaseUtils.getHelper().rawQuery(DeviceDataEntity.class, sql);
    }
    public static List<LinkageLog> findLinkageLogByRange(long ts) {
        String sql = "SELECT * FROM LinkageLog  WHERE ts >= '" + ts + "'";
        return DatabaseUtils.getHelper().rawQuery(LinkageLog.class, sql);
    }
    public static List<DeviceDataEntity> findDataByRangeAndKey(long ts, String key, String value) {
        String sql = "SELECT * FROM DeviceDataEntity  WHERE ts >= '" + ts + "' AND " + key + " = '" + value + "'";

        return DatabaseUtils.getHelper().rawQuery(DeviceDataEntity.class, sql);
    }
    public static LinkageEntity findLinkage(String key, String value) {
        List<LinkageEntity> list = DatabaseUtils.getHelper().queryByKey(LinkageEntity.class, key, value);

        if (list.size() == 0) {            ;
            return null;
        }
        return list.get(0);
    }

    public static List<DeviceDataEntity> findDataByRange(long start, long end) {
        String sql = "SELECT * FROM DeviceDataEntity  WHERE ts BETWEEN '" + start + "' AND '" + end + "'";

        return DatabaseUtils.getHelper().rawQuery(DeviceDataEntity.class, sql);
    }

    public static DeviceDataEntity findLastData(String deviceId) {
        String sql = "SELECT * FROM DeviceDataEntity  WHERE deviceId ='" + deviceId + "'ORDER BY ts DESC";

        return DatabaseUtils.getHelper().rawQueryOne(DeviceDataEntity.class, sql);
    }

    public static List<DeviceDataEntity> findDataByRangeAndKey(long start, long end, String key, String value) {
        String sql = "SELECT * FROM DeviceDataEntity  WHERE ts BETWEEN '" + start + "' AND '" + end + "' AND WHERE " + key + " = " + value;

        return DatabaseUtils.getHelper().rawQuery(DeviceDataEntity.class, sql);
    }

    public static List<DeviceDataEntity> findByKey(String key, String value) {
        return DatabaseUtils.getHelper().queryByKey(DeviceDataEntity.class, key, value);
    }

    public static DeviceRealtimeDataEntity findRealDataByKey(String key, String value) {
        List<DeviceRealtimeDataEntity> list = DatabaseUtils.getHelper().queryByKey(DeviceRealtimeDataEntity.class, key, value);

        if (list.size() == 0) {            ;
            return null;
        }
        return list.get(0);
    }


    public static void clearData() {
        DatabaseUtils.getHelper().clear(DeviceDataEntity.class);
    }

    public static void clearLinkage() {
        DatabaseUtils.getHelper().clear(LinkageEntity.class);
    }

}
